package com.example.crawler.component;

import com.example.crawler.entity.SchoolInfoEntity;
import com.example.crawler.mapper.SchoolInfoMapper;
import com.example.crawler.utils.GsonUtil;
import com.example.crawler.utils.StrUtil;
import com.xiaoleilu.hutool.http.Header;
import com.xiaoleilu.hutool.http.HttpRequest;
import org.apache.ibatis.annotations.Mapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * com.example.crawler.component
 *
 * @author 白雨浓
 * date: 17-10-28 下午2:41
 * content:
 */
@Component
public class SchoolInfoComponent {
    private static final String USER_AGENT = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0";
    private static final String COOKIE = "UM_distinctid=15f620b167d0-0f5b9099fc89a2-72236751-100200-15f620b167e297; CNZZDATA1255363830=60405890-1509173276-null%7C1509201571; Hm_lvt_fd9bf00761167d2e9689ba3d37779881=1509192609,1509192629,1509200287,1509202595; w_phone=d76bpypD9w0f3s1vRktAhAKyDIsXY1Y4uIfTEQHKuOkdWYeBInaZow; w_password=9846nRmCLVnd59BUsj%2FMRXsdQItv91%2FO6Db2l2v%2BJmH4d4JVM3GGIt2Py2U; w_tobe=2f6aw%2FNH5kurzjK0CbGdvTNKlYQGoFQL6TVhyOs; PHPSESSID=2f1roqv8vueei9jg3girvtec30; Hm_lpvt_fd9bf00761167d2e9689ba3d37779881=1509203502; w_liulan=1%2C%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6%2Ccollege2%2F5948cbfc5b57c.jpg*359%2C%E5%86%85%E8%92%99%E5%8F%A4%E6%B0%91%E6%97%8F%E5%A4%A7%E5%AD%A6%2Ccollege2%2F5948cc5a31c00.jpg*360%2C%E5%8C%97%E4%BA%AC%E7%AC%AC%E4%BA%8C%E5%A4%96%E5%9B%BD%E8%AF%AD%E5%AD%A6%E9%99%A2%2Ccollege2%2F5948cc5a31c3d.jpg*361%2C%E5%8D%97%E4%BA%AC%E5%AE%A1%E8%AE%A1%E5%A4%A7%E5%AD%A6%2Ccollege2%2F5948cc5a31c7b.jpg";

    private static Map<Integer, String> map = new HashMap<>();

    @Autowired
    SchoolInfoMapper mapper;

    public void start() {
        handleHtml(3338, 3500);
        map.forEach((k, v) -> System.out.println(k + " " + v));
    }

    public void handleHtml(int start, int end) {
        for (int i = start; i < end; i++) {
            SchoolInfoEntity school = new SchoolInfoEntity();

            String ip = "181" + "." + new Random().nextInt(254) + "." + new Random().nextInt(254) + "." + new Random().nextInt(254);
            String url = "http://www.gaokaoq.com/college/view/id/" + (i + 1) + ".html";

            System.out.println(ip);
            String html = HttpRequest.get(url)
                    .header(Header.USER_AGENT, USER_AGENT)
                    .header(Header.COOKIE, COOKIE)
                    .header("X-Forwarded-For", ip)
                    .header("Cache-Control", "max-age=0")
                    .header("Upgrade-Insecure-Requests", "1")
                    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                    .execute().body();

            Document doc = Jsoup.parse(html);

            /*学校名称*/
            {
                Elements elements = doc.select("div.e_name");
                if (elements.size() == 0) continue;
                Element element = elements.get(0);

                school.setSchoolName(element.text()
                        .replace("关注", "")
                        .replace("已关注", "")
                        .replace(" ", ""));
            }


            /*tags*/
            {
                Elements elements = doc.select("div.c_labels");
                if (elements.size() != 0) {
                    Element element = elements.get(0);
                    String[] tags = element.text().split(" ");
                    List<String> list = new ArrayList<>();
                    for (String tag : tags) {
                        if (!StrUtil.isBlank(tag))
                            list.add(tag);
                    }
                    school.setTags(GsonUtil.toJson(list));
                }
            }

            /*attributes*/
            {
                Elements elements = doc.select("div.attributes");
                if (elements.size() != 0) {
                    Element element = elements.get(0);
                    String[] attrs = element.text().split(" ");
                    if (attrs.length == 9) {
                        try {
                            school.setCreateYear(attrs[0].split("：")[1].replace(" ", ""));
                        } catch (Exception ignored) {
                        }
                        school.setKeySubject(attrs[1].split("：")[1].replace(" ", ""));
                        school.setPhD(attrs[2].split("：")[1].replace(" ", ""));
                        school.setMD(attrs[3].split("：")[1].replace(" ", ""));
                        try {
                            school.setBelong(attrs[4].split("：")[1].replace(" ", ""));
                        } catch (Exception ignored) {

                        }
                        school.setNA(attrs[5].split("：")[1].replace(" ", ""));
                        try {
                            school.setStdNum(attrs[6].split("：")[1].replace(" ", ""));
                        } catch (Exception ignored) {

                        }
                        try {
                            school.setTel(attrs[7].split("：")[1].replace(" ", ""));
                        } catch (Exception ignored) {

                        }
                        school.setAddress(attrs[8].replace(" ", ""));
                    } else if (attrs.length == 10) {
                        school.setCreateYear(attrs[0].split("：")[1].replace(" ", ""));
                        school.setKeySubject(attrs[1].split("：")[1].replace(" ", ""));
                        school.setPhD(attrs[2].split("：")[1].replace(" ", ""));
                        school.setMD(attrs[3].split("：")[1].replace(" ", ""));
                        school.setBelong(attrs[4].split("：")[1].replace(" ", ""));
                        try {
                            school.setNA(attrs[5].split("：")[1].replace(" ", ""));
                        } catch (Exception ignored) {

                        }
                        try {
                            school.setStdNum(attrs[6].split("：")[1].replace(" ", ""));
                        } catch (Exception ignored) {

                        }
                        try {
                            school.setArt(attrs[7].split("：")[1].replace(" ", ""));
                        } catch (Exception ignored) {

                        }
                        try {
                            school.setTel(attrs[8].split("：")[1].replace(" ", ""));
                            school.setAddress(attrs[9].replace(" ", ""));
                        } catch (Exception ignored) {
                            map.put(i + 1, school.getSchoolName());
                        }
                    } else {
                        map.put(i + 1, school.getSchoolName());
                    }
                }
            }

            /*synopsis*/
            {
                Elements elements = doc.select("div.sur_content");
                if (elements.size() != 0) {
                    Element element = elements.get(0);
                    school.setSynopsis(element.html());
                }
            }

            try {
                mapper.insert(school);
                System.out.println(">>" + (i + 1) + "<< 插入学校：" + school.getSchoolName());
            } catch (Exception e) {
                System.err.println(school.toString());
                map.put(i + 1, school.getSchoolName());
                e.printStackTrace();
            }
        }
    }
}
